package com.hmkj.core.mapper.amount;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.amount.AccountModel;
import com.hmkj.core.po.amount.Account;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 账户相关Dao接口<br>
 *
 * @author donfy
 * @date 2017-08-15 03:10:41
 */
public interface AccountMapper extends GenericMapper<Account, Integer> {

    @Select("SELECT count(1) FROM fm_account t LEFT JOIN biz_user u ON t.user_id = u.id  WHERE 1=1 ${whereSql} ${sortSql} ")
    long countBySqlJoin(Map<String, Object> params);

    @Select("SELECT t.*,u.user_name AS 'u.userName',u.mobile AS 'u.mobile' FROM fm_account t LEFT JOIN biz_user u ON t.user_id = u.id WHERE 1=1 ${whereSql} ${sortSql} limit ${nowPage}, ${pageSize} ")
    List<AccountModel> findListBySqlJoin(Map<String, Object> params);

    /**
     * 根据用户ID 查询用户账户信息
     *
     * @param userId 用户ID
     * @return
     */
    @Select("SELECT * FROM fm_account WHERE user_id = #{userId} and del_flag = 1")
    Account selectByUID(@Param(value = "userId") int userId);

    /**
     * 根据用户双乾账户标识 查询用户账户信息
     *
     * @param mmmId 用户双乾账户标识
     * @return
     */
    @Select("SELECT * FROM fm_account WHERE 1=1 and mmm_id = #{mmmId} and del_flag = 1")
    Account selectByMmmId(@Param(value = "mmmId") String mmmId);

    /**
     * 根据用户ID 更新用户账户资金
     *
     * @param total     总额变化量
     * @param available 可用变化量
     * @param freeze    冻结变化量
     * @param userId    用户ID
     */
    @Update("UPDATE fm_account SET total = total + (#{total}), available = available + (#{available}), freeze = freeze + (#{freeze})," +
            "update_time = now() WHERE  user_id = #{userId} AND total + (#{total}) >= 0 AND available + (#{available}) >= 0 AND freeze + (#{freeze}) >= 0")
    int updateAccount(@Param(value = "total") BigDecimal total,
                       @Param(value = "available") BigDecimal available,
                       @Param(value = "freeze") BigDecimal freeze,
                       @Param(value = "userId") Integer userId);

    /**
     * 根据用户ID 更新平台账户资金（不校验金额正负）
     *
     * @param total     总额变化量
     * @param available 可用变化量
     * @param freeze    冻结变化量
     * @param userId    用户ID
     */
    @Update("UPDATE fm_account SET total = total + (#{total}), available = available + (#{available}), freeze = freeze + (#{freeze})," +
            "update_time = now() WHERE  user_id = #{userId}")
    int updateAccount4Plat(@Param(value = "total") BigDecimal total,
                      @Param(value = "available") BigDecimal available,
                      @Param(value = "freeze") BigDecimal freeze,
                      @Param(value = "userId") Integer userId);

    /**
     * 根据用户类型统计可用金额
     * @param param
     * @return
     */
   @Select("<script> SELECT SUM(t.available) FROM fm_account t LEFT JOIN biz_user user ON t.user_id = user.id WHERE user.type = #{type} AND t.del_flag = 1 " +
           "<if test=\"startTime != null and startTime != ''\"> AND user.`create_time` >= #{startTime} </if>" +
           "<if test=\"endTime != null and endTime != ''\"> AND user.`create_time` &lt;= #{endTime} </if>" +
           "<if test=\"agentId != null and agentId != ''\"> AND user.`agent_id` = #{agentId} </if> </script>")
    BigDecimal selectSumAvailableByType(Map<String, Object> param);

    /**
     * 根据用户类型统计冻结金额
     * @param param
     * @return
     */
    @Select("<script> SELECT SUM(t.freeze) FROM fm_account t LEFT JOIN biz_user user ON t.user_id = user.id WHERE user.type = #{type} AND t.del_flag = 1 " +
            "<if test=\"startTime != null and startTime != ''\"> AND user.`create_time` >= #{startTime} </if>" +
            "<if test=\"endTime != null and endTime != ''\"> AND user.`create_time` &lt;= #{endTime} </if>" +
            "<if test=\"agentId != null and agentId != ''\"> AND user.`agent_id` = #{agentId} </if> </script>")
    BigDecimal selectSumFreezeByType(Map<String, Object> param);

    /**
     * 查询符合条件的投资人账户（账户可用金额大于等于止盈保证金）
     *
     * @param profitDeposit 止盈保证金
     * @return
     */
    @Select("SELECT a.* FROM fm_account a LEFT JOIN biz_investor i ON a.user_id = i.user_id LEFT JOIN biz_user u ON a.user_id = u.id WHERE ISNULL(i.id)!=1 AND i.status = 1 AND u.agent_id = #{agentId} AND a.available >= #{profitDeposit} AND  a.del_flag = 1")
    List<Account> selectInvestAccount(@Param(value = "agentId") Integer agentId,@Param(value = "profitDeposit") BigDecimal profitDeposit);
}
